package bus;

import java.util.ArrayList;

import dao.AnswersDao;
import dao.QuestionsDao;
import dao.StudentAnswerDao;
import dao.StudentResultDao;
import dto.AnswersDto;
import dto.QuestionsDto;
import dto.QuizzesDto;
import dto.StudentAnswerDto;
import dto.StudentResultDto;

public class StudentResultBus {

	public static ArrayList<StudentResultDto> getResultBySubject(
			String studentId, String subjectId) {

		ArrayList<StudentResultDto> listResultBySubjectId = new ArrayList<>();
		StudentResultDao dao = new StudentResultDao();

		ArrayList<QuizzesDto> arrQuizz = QuizzesBus
				.getQuizzesBySubjectId(subjectId);

		ArrayList<StudentResultDto> listAllResult = dao.getAllStudentResult();
		for (StudentResultDto studentResultDto : listAllResult) {
			for (QuizzesDto quizz : arrQuizz) {
				if (quizz.getQuizNo() == studentResultDto.getQuizNo()
						&& studentResultDto.getStudentId().equals(studentId))
					listResultBySubjectId.add(studentResultDto);
			}
		}

		return listResultBySubjectId;
	}

	public static void markResultOfStudent(String studentId, int quizNo) {
		
		StudentResultDao studentResultDao = new StudentResultDao();
		QuestionsDao questionsDao = new QuestionsDao();
		ArrayList<QuestionsDto> allQuestion = questionsDao.getAllQuestion();
		ArrayList<QuestionsDto> arrQuestion = new ArrayList<>();
		for (QuestionsDto questionsDto : allQuestion) {
			if (questionsDto.getQuizNo() == quizNo)
				arrQuestion.add(questionsDto);
		}

		StudentAnswerDao studentAnswerDao = new StudentAnswerDao();
		ArrayList<StudentAnswerDto> allAnswer = studentAnswerDao
				.getAllStudentAnswer();
		ArrayList<StudentAnswerDto> arrAnswer = new ArrayList<>();
		for (StudentAnswerDto answer : allAnswer) {
			if (answer.getStudentId().equals(studentId))
				arrAnswer.add(answer);
		}

		int mark = 0;
		for (StudentAnswerDto answer : arrAnswer) {
			AnswersDao answersDao = new AnswersDao();
			ArrayList<AnswersDto> allAnswers = answersDao.getAllAnswer();
			//ArrayList<AnswersDto> arrAnswers = new ArrayList<>();
			for (AnswersDto answerTemp : allAnswers) {
				if (answerTemp.getAnswer() == answer.getAnswer()
						&& answerTemp.getQuestionNo() == answer.getQuestionNo())
					mark++;
			}
		}

		StudentResultDto studentResultDto = new StudentResultDto(quizNo,
				studentId, "", mark + "/" + arrQuestion.size(), "");

		studentResultDao.insertStudentResult(studentResultDto);
	}

}
